package com.example.goband.Config;

import com.example.goband.api.FightApi;
import com.example.goband.api.GameAPI;
import com.example.goband.api.MatchAPI;
import com.example.goband.api.TestAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

import javax.annotation.Resource;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 22479
 * Date: 2023-05-31
 * Time: 19:49
 */

/**
 * 注册websocket类
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
    @Autowired
    private TestAPI testAPI;

    @Autowired
    private MatchAPI matchAPI;

    @Autowired
    private GameAPI gameAPI;

    @Autowired
    private FightApi fightApi;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(testAPI, "/socket").addInterceptors(new HttpSessionHandshakeInterceptor());
        registry.addHandler(matchAPI, "/findMatch").addInterceptors(new HttpSessionHandshakeInterceptor());
        registry.addHandler(gameAPI, "/game").addInterceptors(new HttpSessionHandshakeInterceptor());
        registry.addHandler(fightApi, "/fight").addInterceptors(new HttpSessionHandshakeInterceptor());
        //.addInterceptors(new HttpSessionHandshakeInterceptor())操作
        /**
         * 用户登录就会给HttpSession中保存session信息
         * 这里的addInterceptors相当于把此次用户对话的session拿到我们的websocket的Session中
         */

    }
}
